package com.aaa.statistics.service.imp;

import com.aaa.statistics.mapper.RevenueMapper;
import com.aaa.statistics.service.RevenueService;
import com.aaa.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.*;

@Service
public class RevenueServiceImp implements RevenueService {

    @Autowired
    private RevenueMapper revenueMapper;

    @Override
    public Result<Map<String, Object>> getRenData(String[] dateRange) {
        //退费成功数据 包含现金、支付宝类型
        String beforeDay="";
        String afterDay="";
        if (Objects.nonNull(dateRange)&&dateRange.length==2){
            beforeDay = dateRange[0];
            afterDay = dateRange[1];
        }else {   //当天时间
            beforeDay = LocalDate.now().minusDays(1).toString();
            afterDay = LocalDate.now().plusDays(1).toString();
        }
        //退费成功数据 包含现金、支付宝类型
        List<Map<String,Object>>  backFeeData =revenueMapper.backFeeData(beforeDay,afterDay);
        //收费成功数据 包含现金、支付宝类型
        List<Map<String,Object>> chargeData = revenueMapper.chargeData(beforeDay,afterDay);
        if (Objects.nonNull(backFeeData)&&Objects.nonNull(chargeData)){
            //收入总金额
            int toll=0;
            //总退费
            int refund=0;
            //合计收入
            int totalRevenue=0;
            //现金收入
            int cashIncome=0;
            //支付宝收入
            int alipayIncome=0;
            //现金退费
            int cashRefund=0;
            //支付宝退费
            int alipayRefund=0;
            for (Map<String, Object> backFeeDatum : backFeeData) {
                Object back_amount = backFeeDatum.get("back_amount");
                int backAmount=Integer.parseInt(back_amount.toString());
                //总退费
                refund+=backAmount;
                Object back_type = backFeeDatum.get("back_type");
                int backType=Integer.parseInt(back_type.toString());
                if (backType==0){
                    //现金退费
                    cashRefund+=backAmount;
                }else {
                    //支付宝退费
                    alipayRefund+=backAmount;
                }
            }
            for (Map<String, Object> chargeDatum : chargeData) {
                Object order_amount = chargeDatum.get("order_amount");
                int orderAmount=Integer.parseInt(order_amount.toString());
                //总退费
                toll+=orderAmount;
                Object pay_type = chargeDatum.get("pay_type");
                int backType = Integer.parseInt(pay_type.toString());
                if (backType==0){
                    //现金收费
                    cashIncome+=orderAmount;
                }else {
                    //支付宝收费
                    alipayIncome+=orderAmount;
                }
            }
            totalRevenue=toll-refund;

            Map<String,Object> channel = new HashMap<>();
            channel.put("cashIncome",cashIncome);
            channel.put("alipayIncome",alipayIncome);
            channel.put("cashRefund",cashRefund);
            channel.put("alipayRefund",alipayRefund);

            Map<String,Object> overview = new HashMap<>();
            overview.put("toll",toll);
            overview.put("refund",refund);

            Map<String,Object> revenueObj = new HashMap<>();
            revenueObj.put("totalRevenue",totalRevenue);
            revenueObj.put("channel",channel);
            revenueObj.put("overview",overview);
            // 收支概况
            List<Map<String,Object>> datai = new ArrayList<>();
            Map<String,Object> datai1 = new HashMap<>();
            datai1.put("value",toll);
            datai1.put("name","收费金额");
            Map<String,Object> datai2 = new HashMap<>();
            datai2.put("value",refund);
            datai2.put("name","退费金额");
            datai.add(datai1);
            datai.add(datai2);
            Map<String,Object> revenueOverview = new HashMap<>();
            revenueOverview.put("title","收支概况");
            revenueOverview.put("data",datai);
            // 收入渠道
            List<Map<String,Object>> datar = new ArrayList<>();
            Map<String,Object> datar1 = new HashMap<>();

            datar1.put("value",cashIncome);
            datar1.put("name","现金收入");
            Map<String,Object> datar2 = new HashMap<>();
            datar2.put("value",alipayIncome);
            datar2.put("name","支付宝收入");
            datar.add(datar1);
            datar.add(datar2);
            Map<String,Object> incomeChanel = new HashMap<>();
            incomeChanel.put("title","收支渠道");
            incomeChanel.put("data",datar);
            //退款金额和渠道
            List<Map<String,Object>> datac = new ArrayList<>();
            Map<String,Object> datac1 = new HashMap<>();
            datac1.put("value",cashRefund);
            datac1.put("name","现金退费");

            Map<String,Object> datac2 = new HashMap<>();
            datac2.put("value",alipayRefund);
            datac2.put("name","支付宝退费");
            datac.add(datac1);
            datac.add(datac2);

            Map<String,Object> refund1 = new HashMap<>();
            refund1.put("title","退款");
            refund1.put("data",datac);
            //返回对象
            Map<String,Object> map = new HashMap<>();
            map.put("revenueObj",revenueObj);
            map.put("revenueOverview",revenueOverview);
            map.put("incomeChanel",incomeChanel);
            map.put("refund",refund1);

            return new Result<>(200,"查询成功",map);
        }
        return new Result<>(200,"查询成功",null);
    }
}
